﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace Pibby.Core.Tests
{
	public class StoredClass
	{
		public StoredClass()
		{
		}
	}

	[TestClass]
	public class NotifyingCollectionTests
	{
		private int NotificationCount = 0;

		[TestInitialize]
		public void Setup()
		{
			this.NotificationCount = 0;
		}

		[TestMethod]
		public void OneNotificationForSingleInsert()
		{
			Assert.AreEqual(0, this.NotificationCount, "Count should be zero before test");

			NotifyingCollection<StoredClass> collection = new NotifyingCollection<StoredClass>();

			collection.CollectionChanged += new EventHandler(collection_CollectionChanged);
			collection.Add(new StoredClass()); // 1 update

			Assert.AreEqual(1, this.NotificationCount);
		}

		[TestMethod]
		public void TwoNotificationsForInsertAndClear()
		{
			Assert.AreEqual(0, this.NotificationCount, "Count should be zero before test");

			NotifyingCollection<StoredClass> collection = new NotifyingCollection<StoredClass>();

			collection.CollectionChanged += new EventHandler(collection_CollectionChanged);
			collection.Add(new StoredClass()); // 1 update

			collection.Clear(); // 2

			Assert.AreEqual(2, this.NotificationCount);
		}

		[TestMethod]
		public void BulkNotificationSendsOneNotificationAtEnd()
		{
			Assert.AreEqual(0, this.NotificationCount, "Count should be zero before test");

			NotifyingCollection<StoredClass> collection = new NotifyingCollection<StoredClass>();

			collection.CollectionChanged += new EventHandler(collection_CollectionChanged);

			collection.BulkUpdating = true;

			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());

			collection.Clear();

			collection.BulkUpdating = false; // 1 update

			Assert.AreEqual(1, this.NotificationCount);
		}

		[TestMethod]
		public void UpdatesReceivedNormallyAfterBulkNotificationOff()
		{
			Assert.AreEqual(0, this.NotificationCount, "Count should be zero before test");

			NotifyingCollection<StoredClass> collection = new NotifyingCollection<StoredClass>();

			collection.CollectionChanged += new EventHandler(collection_CollectionChanged);

			collection.BulkUpdating = true;

			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());
			collection.Add(new StoredClass());

			collection.BulkUpdating = false; // 1 update

			collection.Clear(); // 2
			collection.Add(new StoredClass()); // 3

			Assert.AreEqual(3, this.NotificationCount);
		}

		void collection_CollectionChanged(object sender, EventArgs e)
		{
			++this.NotificationCount;
		}
	}
}
